package com.qkl.xiatang.app.utils;

import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Map;

public class MD5Util {

    private static final String SIGN_KEY = "qkl_wvp_admin:admin";

    public static void setSign(Map<String,Object> params){
        MD5SignDTO dto = md5Sign();
        params.put("timestamp",dto.getTimestamp());
        params.put("sign",dto.getSign());
    }

    public static void setSign2(Map<String,String> params){
        MD5SignDTO dto = md5Sign();
        params.put("timestamp",dto.getTimestamp());
        params.put("sign",dto.getSign());
    }

    private static String md5Digest(String source){
        return DigestUtils.md5Hex(source);
    }

    private static MD5SignDTO md5Sign(){
        String timestamp = String.valueOf(System.currentTimeMillis());
        String sign = md5Digest(SIGN_KEY + timestamp);
        MD5SignDTO dto = new MD5SignDTO();
        dto.setSign(sign);
        dto.setTimestamp(timestamp);
        return dto;
    }

    @Data
    private static class MD5SignDTO{

        private String timestamp;

        private String sign;

    }
}
